package timing.ukulele.third.controller.weixin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import org.springframework.web.bind.annotation.*;
import timing.ukulele.common.data.ResponseData;
import timing.ukulele.data.third.data.weixin.WxMsgDTO;
import timing.ukulele.data.third.view.weixin.WxMsgVO;
import timing.ukulele.third.service.weixin.WxMsgService;

/**
 * 微信消息管理
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/weixin/wxMsg")
public class WxMsgController {

    private final WxMsgService wxMsgService;

    /**
     * /wxMsg/page
     * @param wxMsgDTO
     * @return
     */
	@Operation(summary = "分页列表")
    @PostMapping("/page")
    public ResponseData<IPage<WxMsgVO>> getWxMsgPage(@RequestBody WxMsgDTO wxMsgDTO) {
    	return ResponseData.success(wxMsgService.getWxMsgPage(wxMsgDTO));
    }

    /**
     * /wxMsg/clearNotRead
     * @param wxMsgDTO
     * @return
     */
    @Operation(summary = "清楚未读消息")
    @PostMapping("/clearNotRead")
    public ResponseData<Boolean> clearNotRead(@RequestBody WxMsgDTO wxMsgDTO) {
        return ResponseData.success(wxMsgService.clearNotRead(wxMsgDTO));
    }

    /**
     * /wxMsg/getDetails
     * @param id
     * @return
     */
	@Operation(summary = "通过id查询微信消息")
    @GetMapping("/getDetails")
    public ResponseData<WxMsgVO> getDetails(@RequestParam("id") Long id) {
    	return ResponseData.success(wxMsgService.getDetails(id));
    }

    /**
     * /wxMsg/saveWxMsg
     * @param wxMsgDTO
     * @return
     */
	@Operation(summary = "新增微信消息")
    @PostMapping("/saveWxMsg")
    public ResponseData<Boolean> saveWxMsg(@RequestBody WxMsgDTO wxMsgDTO) {
        try {
            return ResponseData.success(wxMsgService.saveWxMsg(wxMsgDTO));
        }catch (WxErrorException e) {
            return ResponseData.error("新增微信消息失败："+e.getMessage());
        }
    }

    /**
     * /wxMsg/updateWxMsg
     * @param wxMsgDTO
     * @return
     */
    @Operation(summary = "修改微信消息")
    @PutMapping("/updateWxMsg")
    public ResponseData<Boolean> updateWxMsg(@RequestBody WxMsgDTO wxMsgDTO)  {
        try {
            return ResponseData.success(wxMsgService.saveWxMsg(wxMsgDTO));
        }catch (WxErrorException e) {
            return ResponseData.error("新增微信消息失败："+e.getMessage());
        }
    }

    /**
     * /wxMsg/deleteWxMsg
     * @param id
     * @return
     */
	@Operation(summary = "删除微信消息")
    @DeleteMapping("/deleteWxMsg")
    public ResponseData<Boolean> deleteWxMsg(@RequestParam("id") Long id){
    	return ResponseData.success(wxMsgService.deleteWxMsg(id));
    }

}
